//
//  ShoucangViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/14.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "ShoucangViewController.h"
#import "ShoucangTableViewCell.h"
#import "SCRenTableViewCell.h"
#import "PersonDetailViewController.h"
#import "HomeDetailViewController.h"

@interface ShoucangViewController ()<UITableViewDataSource,UITableViewDelegate,userJoDelegate>
{
    UITableView * contentTableView;
    UIButton * button1;
    UIButton * button2;
    UIButton * button3;
    UIButton * button4;
    UIView * indicator;
    NSArray * dataArr;
}
@end

@implementation ShoucangViewController


-(void)requestData
{
    if (button1.selected)
    {
        [NetRequestCenter getppfavoritelistwithPage:@"" Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
    else if (button2.selected)
    {
        [NetRequestCenter getaafavoritelistwithPage:@"" type:@"0" Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
    else if (button3.selected)
    {
        [NetRequestCenter getaafavoritelistwithPage:@"" type:@"1" Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
    else
    {
        [NetRequestCenter getccfavoritelistwithPage:@"" Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.title = @"收藏";
    
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth/4, 50)];
    [button1 addTarget:self action:@selector(button1Action) forControlEvents:UIControlEventTouchUpInside];
    button1.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button1 setTitleColor:themeColor forState:UIControlStateSelected];
    [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button1 setTitle:@"人才" forState:UIControlStateNormal];
    [self.view addSubview:button1];
    
    button2 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4, rectStatus.size.height + rectNav.size.height, ScreenWidth/4, 50)];
    [button2 addTarget:self action:@selector(button2Action) forControlEvents:UIControlEventTouchUpInside];
    button2.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button2 setTitleColor:themeColor forState:UIControlStateSelected];
    [button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button2 setTitle:@"活动" forState:UIControlStateNormal];
    [self.view addSubview:button2];
    
    button3 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4 * 2, rectStatus.size.height + rectNav.size.height, ScreenWidth/4, 50)];
    [button3 addTarget:self action:@selector(button3Action) forControlEvents:UIControlEventTouchUpInside];
    button3.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button3 setTitleColor:themeColor forState:UIControlStateSelected];
    [button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button3 setTitle:@"实践" forState:UIControlStateNormal];
    [self.view addSubview:button3];
    
    button4 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4 * 3, rectStatus.size.height + rectNav.size.height, ScreenWidth/4, 50)];
    [button4 addTarget:self action:@selector(button4Action) forControlEvents:UIControlEventTouchUpInside];
    button4.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button4 setTitleColor:themeColor forState:UIControlStateSelected];
    [button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button4 setTitle:@"企业" forState:UIControlStateNormal];
    [self.view addSubview:button4];
    
    indicator = [[UIView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height + 48, 50, 2)];
    indicator.backgroundColor = themeColor;
    [self.view addSubview:indicator];
    
    indicator.center = CGPointMake(ScreenWidth/8, indicator.center.y);
    button1.selected = YES;
    
    contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height + 50, ScreenWidth, ScreenHeight - rectStatus.size.height - rectNav.size.height - 50) style:UITableViewStylePlain];
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:contentTableView];
    
    [self requestData];
}

-(void)button1Action
{
    button1.selected = YES;
    button2.selected = NO;
    button3.selected = NO;
    button4.selected = NO;
    indicator.center = CGPointMake(ScreenWidth/8, indicator.center.y);
    [self requestData];
}

-(void)button2Action
{
    button1.selected = NO;
    button2.selected = YES;
    button3.selected = NO;
    button4.selected = NO;
    indicator.center = CGPointMake(ScreenWidth/8 * 3, indicator.center.y);
    [self requestData];
}

-(void)button3Action
{
    button1.selected = NO;
    button2.selected = NO;
    button3.selected = YES;
    button4.selected = NO;
    indicator.center = CGPointMake(ScreenWidth/8 * 5, indicator.center.y);
    [self requestData];
}

-(void)button4Action
{
    button1.selected = NO;
    button2.selected = NO;
    button3.selected = NO;
    button4.selected = YES;
    indicator.center = CGPointMake(ScreenWidth/8 * 7, indicator.center.y);
    [self requestData];
}

#pragma mark - tableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArr count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (button1.selected || button4.selected)
    {
        return [SCRenTableViewCell height];
    }
    else
    {
        return [ShoucangTableViewCell height];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (button1.selected || button4.selected)
    {
        static NSString * identifer = @"identifer";
        SCRenTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[SCRenTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        [cell loadData:[dataArr objectAtIndex:indexPath.row]];
        return cell;
    }
    else
    {
        static NSString * identifer = @"sd";
        ShoucangTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[ShoucangTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        cell.delegate = self;
        [cell loadData:[dataArr objectAtIndex:indexPath.row]];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (button1.selected || button4.selected)
    {
        PersonDetailViewController * per = [[PersonDetailViewController alloc]init];
        if ([[dataArr objectAtIndex:indexPath.row]objectForKey:@"FriendUID"])
        {
            per.uid = [[dataArr objectAtIndex:indexPath.row]objectForKey:@"FriendUID"];
        }
        else
        {
            per.uid = [[dataArr objectAtIndex:indexPath.row]objectForKey:@"CompanyUID"];
        }
        [self.navigationController pushViewController:per animated:YES];
    }
    else
    {
        HomeDetailViewController * det = [[HomeDetailViewController alloc]init];
        det.contentid = [[dataArr objectAtIndex:indexPath.row]objectForKey:@"TaskID"];
        [self.navigationController pushViewController:det animated:YES];
    }
}

-(void)user:(NSString *)user
{
    PersonDetailViewController * per = [[PersonDetailViewController alloc]init];
    per.uid = user;
    [self.navigationController pushViewController:per animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
